package cn.itsuorce.controller;

import cn.itsuorce.utils.AjaxResult;
import cn.itsuorce.utils.FastDfsUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastdfs")
public class FastdfsController {


    /**
     * 文件上传接口
     * @param shopLogo
     * @return
     */
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile shopLogo){
        try {
            //真实文件名称
            String filename = shopLogo.getOriginalFilename();
            //获取后缀 不要那个点
            String extName = filename.substring(filename.lastIndexOf(".") + 1);
            //保存到fastdfs文件服务器中
            String fileid = FastDfsUtils.upload(shopLogo.getBytes(), extName);
            System.out.println(fileid);
            //通过AjaxResult对象的data属性，设置以后，返回给前端
            return AjaxResult.me().setData(fileid);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("文件上传失败!" + e.getMessage());
        }
    }
}
